package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.maintain.X3AloneServiceAdapter;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.bean.greendao.AloneServiceNewBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.mvp.view.create.fragment.X3AddServiceAlreadySelectedWorkAdapter;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class X3BottomAlreadySelectDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3AddServiceAlreadySelectedWorkAdapter.IOnSelectWorkList {

    private TextView tv_name,tv_clear;
    private RecyclerView rlv_service;
    private X3AddServiceAlreadySelectedWorkAdapter serviceAdapter;
    private LinearLayout ll_clear;
    private X3PointDialog pointDialogall;
    private ImageView iv_clear;

    private List<SelectWorkBean.ServiceBaseEntityBean> sList;

    public X3BottomAlreadySelectDialog(@NonNull Context context,List<SelectWorkBean.ServiceBaseEntityBean> sList,IOnSelectWorkLisener iOnSelectWorkLisener) {
        super(context);
        this.sList = sList;
        this.iOnSelectWorkLisener = iOnSelectWorkLisener;
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        iv_clear = findViewById(R.id.iv_clear);
        tv_clear = findViewById(R.id.tv_clear);
        tv_name = findViewById(R.id.tv_name);
        rlv_service = findViewById(R.id.rlv_service);
        ll_clear = findViewById(R.id.ll_clear);
        ll_clear.setOnClickListener(this);
        rlv_service.setLayoutManager(new LinearLayoutManager(mContext));
        serviceAdapter = new X3AddServiceAlreadySelectedWorkAdapter(mContext,sList);
        rlv_service.setAdapter(serviceAdapter);
        serviceAdapter.setiOnSelectWorkList(this);

        tv_name.setText(String.format(mContext.getString(R.string.alone_select_count,sList.size())));
        clearStatus();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_already_select_service;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_clear ) {
            if (sList!=null && 0 < sList.size()){
                showClearDialog();
            }
        }
    }

    @Override
    public void iOnSelectWork(SelectWorkBean.ServiceBaseEntityBean serviceBean, boolean isDel) {
        if (isDel){
            sList.remove(serviceBean);
            serviceAdapter.changeAddService(sList);
            tv_name.setText(String.format(mContext.getString(R.string.alone_select_count,sList.size())));
            clearStatus();
            if (iOnSelectWorkLisener != null){
                iOnSelectWorkLisener.iOnSelectWork(serviceBean,false);
            }
        }
    }

    private void clearStatus(){
        Drawable tipsArrow = iv_clear.getDrawable();
        if (sList.size() > 0){
            tv_clear.setTextColor(Color.parseColor("#2D3040"));
            tipsArrow.setColorFilter(Color.parseColor("#2D3040"), PorterDuff.Mode.SRC_ATOP);
        } else {
            tv_clear.setTextColor(Color.parseColor("#D7D7DB"));
            tipsArrow.setColorFilter(Color.parseColor("#D7D7DB"), PorterDuff.Mode.SRC_ATOP);
        }
        iv_clear.setImageDrawable(tipsArrow);

    }

    IOnSelectWorkLisener iOnSelectWorkLisener;

    public void setIOnSelectWorkList(IOnSelectWorkLisener iOnSelectWorkLisener) {
        this.iOnSelectWorkLisener = iOnSelectWorkLisener;
    }

    public interface IOnSelectWorkLisener {
        /**
         *
         * @param serviceBean
         * @param isDelOrClear 删除 false，清空 true
         */
        void iOnSelectWork(SelectWorkBean.ServiceBaseEntityBean serviceBean,boolean isDelOrClear);
    }

    private void showClearDialog() {

        pointDialogall = new X3PointDialog(mContext,"",mContext.getString(R.string.alone_del_all_selec),
                mContext.getString(R.string.x3_cancel_info),mContext.getString(R.string.x3_affirm_string));

        pointDialogall.show();
        pointDialogall.setiBtnClick(new X3PointDialog.IBtnClick() {
            @Override
            public void iBtnAffirmClick() {

                if (pointDialogall != null) {
                    pointDialogall.dismiss();
                }
                X3BottomAlreadySelectDialog.this.dismiss();
                if (iOnSelectWorkLisener != null){
                    iOnSelectWorkLisener.iOnSelectWork(null,true);
                }
            }

            @Override
            public void iBtnCancelClick() {
                if (pointDialogall != null) {
                    pointDialogall.dismiss();
                }
            }
        });
    }

}
